import {defineStore, createPinia} from 'pinia'
import {CtrlColor} from '../types/index'

export const useGameStore = defineStore({
    id: 'game',
    state: () => ({
        //颜色面板的颜色集合
        colorList: <CtrlColor[]>[],
        //当前选中的颜色
        colorListActiveItem: <CtrlColor>{},
        //像素点的宽高
        pixelWidth: 10,
        pixelHeight: 10,
    }),
    getters: {
        getPixelSize(): any {
            return {width: this.pixelWidth, height: this.pixelHeight}
        },
        getColorList(): CtrlColor[] {
            return this.colorList;
        },
        selectColor(): string {
            if (this.colorList.length == 0) return '#fff'
            if (this.colorListActiveItem.color) {
                return this.colorListActiveItem.color;
            } else {
                return this.colorList[0].color;
            }
        }
    },
    actions: {
        zoomPixel(lv: number) {
            //限制最小7像素
            if (this.pixelWidth + lv < 7) {
                this.pixelWidth = 7;
                this.pixelHeight = 7;
                return false;
            } else {
                this.pixelWidth += lv;
                this.pixelHeight += lv;
                return true;
            }
        },
        //设置颜色列表
        setColorList(colorList: Array<CtrlColor>) {
            this.colorList = colorList;
            for (let item of this.colorList) {
                if (item.active) {
                    return;
                }
            }
            this.colorList[0].active = true;
        },
        //增加颜色
        addCtrlColor(color: CtrlColor) {
            for (let item of this.colorList) {
                if (item.keyCode == color.keyCode) {
                    return false;
                }
            }
            this.colorList.push(color);
            return true;
        },
        //选中颜色 根据快捷键
        setColorSelect(keyCode: number): number {
            let curSelect: CtrlColor = <CtrlColor>{},
                targetSelect: CtrlColor = <CtrlColor>{},
                curIndex: number = -1,
                targetIndex: number = -1;
            for (let index in this.colorList) {
                let item = this.colorList[index];
                if (item.active) {
                    curIndex = parseInt(index);
                    curSelect = item;
                }
                if (item.keyCode == keyCode) {
                    targetIndex = parseInt(index);
                    targetSelect = item;
                }
                if (curSelect.keyCode && curSelect.keyCode == targetSelect.keyCode) {
                    return targetIndex;
                }
            }
            if (!!targetSelect.color) {
                this.colorListActiveItem = targetSelect;
                targetSelect.active = true;
                if (!!curSelect.color) {
                    curSelect.active = false;
                }
                return targetIndex;
            }
            return -1;
        }
    }
})

export const pinia = createPinia();
